package com.example.lot_lock;

import androidx.annotation.NonNull;
import androidx.appcompat.app.AppCompatActivity;

import android.annotation.SuppressLint;
import android.content.Intent;
import android.os.Bundle;
import android.os.Handler;
import android.os.Message;
import android.provider.ContactsContract;
import android.widget.EditText;
import android.widget.ListView;
import android.widget.SimpleAdapter;
import android.widget.TextView;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.HashMap;
import java.util.List;
import java.util.Map;

import static java.lang.Thread.sleep;

public class user_control extends AppCompatActivity {

    ListView listView = null;
    TextView equipID = null;

    List<HashMap<String, String>>list= null;
    HashMap<String, String> map = null;
    SimpleAdapter simpleAdapter = null;
    int i = 0;

    Handler handler = new Handler(){
        @Override
        public void handleMessage(@NonNull Message msg) {
            super.handleMessage(msg);
        i++;
        SimpleDateFormat simpleDateFormat = new SimpleDateFormat("yyy-MM-dd HH:mm:ss");
        Date date = new Date(System.currentTimeMillis());
        String Time = simpleDateFormat.format(date);
        String Type;
        if(i%3 == 1)
            Type = "指纹";
        else if(i%3 == 2)
            Type = "ID卡";
        else
            Type = "远程";

        map = new HashMap<String, String>();
        map.put("Date", Time + "        " + Type + "                   " + "A" + i*100);
        list.add(0,map);
        listView.setAdapter(simpleAdapter);
        }
    };



    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.user_control);

        listView = findViewById(R.id.usr_control_listview_id);
        equipID = findViewById(R.id.user_control_controlID_id);
        Intent intent = getIntent();
        String equipId = intent.getStringExtra("equipID");
        equipID.setText("设备ID:"+equipId);


        list = new ArrayList<HashMap<String, String>>();
        simpleAdapter = new SimpleAdapter(user_control.this, list, android.R.layout.simple_list_item_1, new String[]{"Date"},
                new int[]{android.R.id.text1});

        new Thread(new Runnable() {
            @Override
            public void run() {

                while(true) {
                    Message message = new Message();
                    message.what = 1;
                    try {
                        sleep(400);
                    } catch (InterruptedException e) {
                        e.printStackTrace();
                    }
                    handler.sendMessage(message);
                }
            }
        }).start();

    }
}